@extends('admin.home.main')

@section('body')
  <style>
    .tanchuang{width:100%;height:100%;position:absolute;top:0;right:0;left:0;bottom:0;opacity:0.3;background:#bbb;
      display:none;}
    .tc_content{width:500px;height:300px;position:absolute;z-index:999;background:#fff; margin:auto;top:0;right:0;
      left:0;bottom:0;display:none;}
  </style>
  <section id="main-content">

    <section class="wrapper">
      <h3>课程列表</h3>
      <hr>
      <div class="row mt">
        <div class="col-md-12">
          <form action="/admin/user/list" method="get">
            <select size="1" name="page_size" aria-controls="hidden-table-info" style="width:80px; height:34px; float:left; " class="form-control">
              <option value="0">请选择</option>
              @foreach($page_size_list as $ptm=>$pst)
                @if($pst == $page_size)
                  <option value="{{$pst}}" selected="selected">{{$pst}}</option>
                @else
                  <option value="{{$pst}}">{{$pst}}</option>
                @endif
              @endforeach
            </select>
            <input type="text" style="width:200px;float:left; margin-left:10px;" class="form-control" placeholder="请课程名称" name="keywords" id="keywords" value="{{$keywords}}">
            <button type="submit" id="search" style="float:left; margin-left:10px;" class="btn btn-success">搜索</button>
          </form>
          <a href="/admin/course/add" class="btn btn-primary" style="float:right;">添加课程</a>
        </div>
      </div>

      <!-- row -->
      <div class="row mt">
        <div class="col-md-12">
          <div class="content-panel">
            <table class="table table-striped table-advance table-hover">
              <thead>
              <tr>
                <th>#</th>
                <th>课程名称</th>
                <th>权重</th>
                <th>集数</th>
                <th>课程类目</th>
                <th>是否收费</th>
                <th>课程价格</th>
                <th>已售数量</th>
                <th>操作</th>
              </tr>
              </thead>
              <tbody>
              @foreach($courselist as $ctm=>$cst)
                <tr>
                  <td>{{$cst->id}}</td>
                  <td>{{$cst->name}}</td>
                  <td>
                    @if($cst->weight > 0)
                      {{$cst->weight}}
                    @else
                      0
                    @endif
                  </td>
                  <td>{{$cst->video_num}}集</td>
                  <td>{{$cst->type_name}}</td>
                  <td>
                    @if($cst->isfree == 0)
                      免费
                    @else
                      不免费
                    @endif
                  </td>
                  <td>{{$cst->price}}</td>
                  <td>
                    @if($cst->sellnum > 0)
                      {{$cst->sellnum}}套
                    @else
                      0套
                    @endif
                  </td>
                  <td>
                    <a class="btn btn-warning btn-xs" href="/admin/course/view_video_list/{{$cst->id}}">查看视频列表</a>
                    <a href="/admin/course/edit/{{$cst->id}}" class="btn btn-primary btn-xs">编辑</a>
                    <a href="javascript:;" colid = "{{$cst->id}}" class="btn btn-xs btn-info
                givecourse">赠送</a>
                    <button class="btn btn-success btn-xs give_course_list" type="button"
                            colid="{{$cst->id}}">查看赠送列表</button>
                    <button type="button" colid="{{$cst->id}}" class="btn btn-danger btn-xs col_del">删除</button>
                  </td>
                </tr>
              @endforeach
              </tbody>
            </table>
          </div>
          <!-- /content-panel -->
          <div class="row-fluid">

            <div class="span6">
              <div class="dataTables_paginate paging_bootstrap pagination">
                <ul>{!! $courselist->render() !!}</ul>
              </div>
            </div>
          </div>
        </div>
        <!-- /col-md-12 -->
      </div>
      <!-- /row -->
    </section>
  </section>
  <div class="tanchuang"></div>
  <div class="tc_content">
    <div style="text-align:right; padding:20px;">
      <span class="glyphicon glyphicon-remove close" aria-hidden="true"></span>
    </div>
    <div class="_content" style="height:200px; overflow-y:scroll; padding-right:20px; padding-left:20px;"></div>
  </div>
  <script type="text/javascript">
    function opcheckboxed(objName, type) {
      var objNameList = document.getElementsByClassName(objName);
      if (null != objNameList) {
        for (var i = 0; i < objNameList.length; i++) {
          if (objNameList[i].checked == true) {
            if (type != 'checkall') { // 非全选
              objNameList[i].checked = false;
            }
          } else {
            if (type != 'uncheckall') { // 非取消全选
              objNameList[i].checked = true;
            }
          }
        }
      }
    }

    $('body').on('click','.give_course',function(){
      var userid = [];
      var courseid = $('#courseid').val();
      var token = $('#_token').val();

      $('.memberid').each(function(){
        if($(this).is(':checked')){
          userid.push($(this).val());
        }
      })
      userid = userid.join(',');
      if(!userid){
        alert('请选择赠课的会员');
        return false;
      }

      $.ajax({
        url:'/admin/course/give_course_handle',
        type:'post',
        data:{'_token':token,'courseid':courseid,'userid':userid},
        success:function(data){
          // return false;
          if(data.code == 200){
            alert('赠课成功');
          }else if(data.code == 300){
            alert('您所选中的会员均已被赠课,请勿重复赠课');
          }else{
            alert('赠课失败');
          }
          location.reload();
        }
      })
    })

    $('.give_course_list').click(function(){
      var _colid = $(this).attr('colid');
      $.ajax({
        url:'/admin/course/give_course_list/'+_colid,
        type:'get',
        dataType:'json',
        success:function(data){
          console.log(data);
          if(data.code==200){
            var html = '<table class="table table-striped table-advance table-hover">';
            var orderlist = data.list;
            for(var i=0; i< orderlist.length; i++){
              html += '<tr><td>'+orderlist[i].nick+'</td><td>'+orderlist[i].telephone+'</td></tr>';
            }
            html += '</table>';
            $('._content').html(html);

            $('.tanchuang').css('display','block');
            $('.tc_content').css('display','block');
          }else{
            alert('暂无赠送列表');
            return false;
          }
        }
      })
    })

    $('.close').click(function(){
      $('.tc_content').css('display','none');
      $('.tanchuang').css('display','none');
    })

    $('.givecourse').click(function(){
      var _colId = $(this).attr('colid');
      $.ajax({
        url:'/admin/course/give_course/'+_colId,
        type:'get',
        dataType:'json',
        success:function(data){
          if(data.code == 200){
            var crsf_token = '{{csrf_token()}}';

            var _html = '<p>备注：已勾选并且用户昵称为红色部分为已赠送用户</p><form action="/admin/course/give_course_handle" ' +
                    'method="post"><p><input ' +
                    'type="hidden"' +
                    ' ' +
                    'name="_token" ' +
                    'id="_token" ' +
                    'value="'+crsf_token+'"><input ' +
                    'type="hidden" ' +
                    'id="courseid" value="'+_colId+'"></p><p><button type="button" class="btn btn-xs btn-default" ' +
                    'onclick="opcheckboxed' +
                    '(\'memberid\', ' +
                    '\'checkall\')">全选</button>\n' +
                    '    <button class="btn btn-xs btn-default" type="button" onclick="opcheckboxed(\'memberid\', \'uncheckall\')">取消全选</button>\n' +
                    '    <button class="btn btn-xs btn-default" type="button" onclick="opcheckboxed(\'memberid\', \'reversecheck\')' +
                    '">反选</button></p><p' +
                    ' ' +
                    'id="checkBoxList">';
            for(var i=0; i<data.member_list['data'].length; i++){
              if(data.member_list['data'][i].is_checked == 1){
                _html += '<input type="checkbox" checked="checked" class="memberid" value="'+data.member_list['data'][i]
                        .id+'">&nbsp;&nbsp;&nbsp;<span style="color:red">'+data
                        .member_list['data'][i].nick+'</span>&nbsp;&nbsp;&nbsp;';
              }else{
                _html += '<input type="checkbox" class="memberid" value="'+data.member_list['data'][i].id+'">&nbsp;&nbsp;&nbsp;'+data
                        .member_list['data'][i].nick+'&nbsp;&nbsp;&nbsp;';
              }

            }
            _html += '</p><p><button type="button" class="btn btn-success btn-xs give_course">点此赠送</button></p></form>';

            $('._content').html(_html);

            $('.tanchuang').css('display','block');
            $('.tc_content').css('display','block');
          }else{
            alert('没有会员列表');
            return false;
          }
        }
      })
    })

    $('.col_del').click(function(){
      var _colId = $(this).attr('colid');

      var a = confirm('请确认是否要删除该课程');
      var t = $(this).parent().parent();

      if(a == true){
        $.ajax({
          'type':'get',
          'url':'/admin/course/del/'+_colId,
          'dataType':'json',
          success:function(msg){
            if(msg.code == 200){
              alert('删除成功');
            }else{
              alert('删除失败');
            }
            location.reload();
          }
        })
      }
    })
  </script>
@endsection